package com.leung.mybatis.binding;


import com.leung.mybatis.session.SqlSession;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * @author 丨一
 * @version 1.0
 * @title MapperProxy
 * @description: 映射器代理类
 * @date 2025/10/8 上午10:06
 */
public class MapperProxy<T> implements InvocationHandler, Serializable {

    private static final long serialVersionUID = -6723412345678901234L;

    private SqlSession sqlSession;

    private final Class<T> mapperInterface;

    public MapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {
        this.mapperInterface = mapperInterface;
        this.sqlSession = sqlSession;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        if (Object.class.equals(method.getDeclaringClass())) {
            return method.invoke(this, args);
        }

        //会输出类的全类名
        //System.out.println(mapperInterface.getName());

        return sqlSession.selectOne(method.getName(), args);
    }

}
